<!DOCTYPE html>
<html lang="UTF-8" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/main.css">
    <link rel="stylesheet" type="text/css" href="/css/pagination.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor:pointer;
        }
        .tree-closed {
            height : 40px;
        }
        .tree-expanded {
            height : auto;
        }
    </style>
</head>

<body>

<div th:replace="~{admin/include-nav::nav}" ></div>
<div class="container-fluid">
    <div class="row">
        <div th:replace="~{admin/include-sidebar::sidebar}"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                        <form th:action="@{/admin/query/for/search}" method="post" class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input th:value="${param.keyword}" name="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;">
                        <i class=" glyphicon glyphicon-remove"></i> 删除</button>
<!--                    <button type="button" class="btn btn-primary" style="float:right;" onclick="window.location.href='add.html'"><i class="glyphicon glyphicon-plus"></i> 新增</button>-->
                    <a th:href="@{/admin/to/add/page.html}" class="btn btn-primary" style="float: right;">
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </a>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr >
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            <tr th:if="${#lists.isEmpty(#request.getAttribute('list'))}">
                                <td style="text-align: center;" colspan="6">抱歉！没有符合您要求的查询结果！</td>
                            </tr>
                            <tr th:each="admin,myStatus : ${list}">
                                <td th:text="${myStatus.count}"></td>
                                <td><input th:adminId="${admin.id}" th:loginAcct="${admin.loginAcct}" class="itemBox" type="checkbox"></td>
                                <td th:text="${admin.loginAcct}"></td>
                                <td th:text="${admin.userName}"></td>
                                <td th:text="${admin.email}"></td>
                                <td>
                                    <a th:href="@{/assign/to/assign/role/page(adminId=${admin.id },pageNum=${pageNum})}" class="btn btn-success btn-xs">
                                        <i class=" glyphicon glyphicon-check"></i>
                                    </a>
                                    <a th:href="@{/admin/to/edit/page(adminId=${admin.id},pageNum=${#request.getAttribute('pageNum')})}" class="btn btn-primary btn-xs">
                                        <i class=" glyphicon glyphicon-pencil"></i>
                                    </a>
<!--                                    <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>-->
                                    <button type="button" class="btn btn-danger btn-xs uniqueRemoveBtn" th:adminId="${admin.id}" th:userName="${admin.userName}" ><i class=" glyphicon glyphicon-remove"></i></button>
                                </td>
                            </tr>

                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/jquery/jquery-2.1.1.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>
<script src="/script/docs.min.js"></script>
<script type="text/javascript" src="/script/jquery.pagination.js"></script>
<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function(){
            if ( $(this).find("ul") ) {
                $(this).toggleClass("tree-closed");
                if ( $(this).hasClass("tree-closed") ) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
    });
    $(function() {
        // 对分页导航条显示进行初始化
        initPagination();
    });

    // 声明函数封装导航条初始化操作
    function initPagination() {
        // 声明变量存储总记录数
        var totalRecord = [[${#request.getAttribute('total')}]];
        // 声明变量存储分页导航条显示时的属性设置
        var paginationProperties = {
            num_edge_entries : 3,        //边缘页数
            num_display_entries : 5,      //主体页数
            callback : pageselectCallback, //回调函数
            items_per_page : [[${#request.getAttribute('pageSize')}]],    //每页显示数据数量，就是pageSize
            current_page : [[${pageNum-1}]],//当前页页码
            prev_text : "上一页",       //上一页文本
            next_text : "下一页"        //下一页文本
        };
        // 显示分页导航条
        $("#Pagination").pagination(totalRecord, paginationProperties);
    }

    // 在每一次点击“上一页”、“下一页”、“页码”时执行这个函数跳转页面
    function pageselectCallback(pageIndex, jq) {
        // pageIndex从0开始，pageNum从1开始
        var pageNum = pageIndex + 1;
        var keyword=$('[name="keyword"]').val();
        // 跳转页面
        window.location.href = "/admin/query/for/search?pageNum="+pageNum+"&keyword="+keyword;
        return false;
    }

    $(function () {
        //全选
        $('#summaryBox').click(function () {
            $('.itemBox').prop("checked", this.checked)
        })

        //批量删除
        $('#batchRemoveBtn').click(function () {
            //收集id和用户名
            var ids=new Array();
            var usernames=new Array();
            $(".itemBox:checked").each(function () {
                var adminId=$(this).attr("adminId");
                ids.push(adminId);
                var loginAcct=$(this).attr("loginAcct")
                usernames.push(loginAcct)
            })
            console.log(ids,usernames)
            //验证
            if (ids.length==0){
                alert("请勾选您要删除的记录！");
                return;
            }
            //弹出确认框
            var isDel=confirm("您真的要删除"+usernames+"信息吗？操作不可逆，请谨慎决定！")
            if (isDel==false){
                return
            }
            //发送ajax请求
            $.ajax({
                url:"/admin/batch/remove",
                "type":"post",
                "data":JSON.stringify(ids),
                "dataType":"json",
                "contentType":"application/json;charset=UTF-8",
                "success":function (response) {
                    if (response.msg=="成功"){
                        var pageNum=[[${pageNum}]]
                        var keyword=$('[name="keyword"]').val()
                        window.location.href="/admin/query/for/search?pageNum="+pageNum+"&keyword="+keyword;
                    }
                }
            })
        })
    })

    $('.uniqueRemoveBtn').click(function () {
        var userList=new Array();
        var adminId=$(this).attr("adminId");
        userList.push(adminId);
        var userName=$(this).attr("userName");
        if (confirm("您真的要删除"+userName+"信息吗？")){
            // alert(userList)
            $.ajax({
                url:"/admin/batch/remove",
                type:"post",
                data:JSON.stringify(userList),
                dataType:"json",
                contentType:"application/json;charset=UTF-8",
                success:function (res) {
                    if (res.msg=="成功"){
                        var pageNum=[[${pageNum}]]
                        var keyword=$('[name="keyword"]').val()
                        window.location.href="/admin/query/for/search?pageNum="+pageNum+"&keyword="+keyword;
                    }
                }
            })
        }
    })
</script>
</body>
</html>
